package com.atguigu.srb.core.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.srb.core.listener.ExcelDictDTOListener;
import com.atguigu.srb.core.pojo.entity.Dict;
import com.atguigu.srb.core.mapper.DictMapper;
import com.atguigu.srb.core.pojo.entity.dto.ExcelDictDTO;
import com.atguigu.srb.core.service.DictService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author lay_hu
 * @since 2021-12-13
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
    @Resource
    RedisTemplate redisTemplate;

    @Transactional(rollbackFor = Exception.class) //设置事物的回滚
    @Override
    public void importData(InputStream inputStream) {
        EasyExcel.read(inputStream, ExcelDictDTO.class, new ExcelDictDTOListener(baseMapper)).sheet().doRead();
    }

    @Override
    public  List<ExcelDictDTO> listDictData() {
        List<Dict> dictsList = baseMapper.selectList(null);
        ArrayList<ExcelDictDTO> excelDictDTOS = new ArrayList<>(dictsList.size());
        dictsList.forEach(dict -> {
            ExcelDictDTO excelDictDTO = new ExcelDictDTO();
            BeanUtils.copyProperties(dict,excelDictDTO);
            excelDictDTOS.add(excelDictDTO);

        });

        return excelDictDTOS;
    }

    @Override
    public List<Dict> listByParentId(Long parentId) {
       //第一次查询redis缓存时 看看有没有list 数据
        try {
            List<Dict>  dictList= (List<Dict>) redisTemplate.opsForValue().get("srb:core:dist:"+parentId);
            if (dictList!=null){

                return dictList;
            }
        } catch (Exception e) {
           log.error("服务器错误"+ ExceptionUtils.getStackTrace(e));
        }

            QueryWrapper<Dict> wrapper = new QueryWrapper<>();
            wrapper.eq("parent_id",parentId);
            List<Dict> dictList = baseMapper.selectList(wrapper);
            dictList.forEach(dict -> {
                boolean hasChildren = this.hasChildren(dict.getId());
                dict.setHasChildren(hasChildren);
            });
        try {
            redisTemplate.opsForValue().set("srb:core:dist:"+parentId,dictList,5, TimeUnit.SECONDS);
        } catch (Exception e) {
           log.error("数据库建设缓存不存在"+ExceptionUtils.getStackTrace(e));
        }
        return dictList;

    }

    public  boolean hasChildren(Long id){
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            return true;
        }else {
            return  false;
        }
    }
}
